package com.aman.common;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import java.util.List;

/**
 * 项目名称  ：ParallaxViewPager
 * 类名称    ：
 * 类描述    ：
 * 创建人    ：Aman/862032054@qq.com
 * 创建时间  ：2016/9/30 17:05
 * 修改人    ：admin
 * 修改时间  ：2016/9/30 17:05
 * 修改备注  ：
 *
 * @version V1.0
 */
public abstract class BaseRecyclerImageAdapter extends BaseRecyclerAdapter {

    public List<?> list;

    private Context context;
    private boolean isScrolling;
    private boolean isFirstLoad = true;

    public BaseRecyclerImageAdapter(Context context, List<?> list) {
        super(context, list);
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(onCreateViewLayoutID(viewType), null);
        //滑动的时候不加载图片
        RecyclerView recyclerView = (RecyclerView) parent;
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //当前的recycleView不滑动(滑动已经停止时)  SCROLL_STATE_IDLE = 0;
                //当前的recycleView被拖动滑动  SCROLL_STATE_DRAGGING = 1;
                //当前的recycleView在滚动到某个位置的动画过程,但没有被触摸滚动   SCROLL_STATE_SETTLING = 2;
                isScrolling = (newState != RecyclerView.SCROLL_STATE_IDLE && newState != RecyclerView.SCROLL_STATE_DRAGGING);
                isFirstLoad = false;
                if (!isScrolling) {
                    notifyDataSetChanged();
                }
            }
        });
        //滑动的时候不加载图片
        return new RecyclerHolder(view);
    }

    public abstract int onCreateViewLayoutID(int viewType);


    @Override
    public void onViewRecycled(final RecyclerHolder holder) {
        super.onViewRecycled(holder);
    }

    public Object getItem(int position) {
        return list.get(position);
    }


    /**
     * @param holder
     * @param position
     * @hide
     */
    @Override
    public void onBindViewHolder(final RecyclerHolder holder, final int position) {
        onBindViewHolder(holder.getViewHolder(), position);
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(null, v, holder.getPosition(), holder.getItemId());
                }
            });
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        onBindView(holder, getItem(position), position);
        if (!isScrolling) {
            onBindImageView(holder, getItem(position), position);
        }

    }

    /**
     * 更新除图片外views并设置ImageView的占位图片
     *
     * @param holder   holder
     * @param item     item
     * @param position position
     */
    public abstract void onBindView(ViewHolder holder, Object item, int position);

    /**
     * 更新图片
     *
     * @param holder   holder
     * @param item     item
     * @param position position
     */
    public abstract void onBindImageView(ViewHolder holder, Object item, int position);


    @Override
    public int getItemCount() {
        return list.size();
    }

    private AdapterView.OnItemClickListener onItemClickListener;

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
